using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Quiki.ExpressionParsers;

namespace Quiki.Templates
{
    public abstract class NVelocityBasedWikiTemplate : IWikiTemplate
    {
        public abstract string TemplateName { get; }
        public abstract void Expand(WikiTemplateElement element, WikiDocument document, DocumentGraph documentGraph);

        public virtual void Format(WikiTemplateElement element, HtmlFormatterContext context)
        {
            string templateFileName = String.Format (
                CultureInfo.InvariantCulture,
                @"Templates\{0}.vm.html",
                element.TemplateName);

            templateFileName = Path.Combine (documentProcessorSettings.InputDir, templateFileName);

            Dictionary<string, object> properties = new Dictionary<string, object> ();
            properties["element"] = element;
            properties["quiki"] = new QuikiInfo ();
            properties["doc"] = context.Document;
            properties["graph"] = context.DocumentGraph;

            string expandedText = templatingEngine.ExpandTemplate (templateFileName, properties);
            context.Writer.InnerWriter.Write (expandedText);            
        }

        protected NVelocityBasedWikiTemplate (ITemplatingEngine templatingEngine, ILogger logger, DocumentProcessorSettings documentProcessorSettings)
        {
            this.templatingEngine = templatingEngine;
            this.logger = logger;
            this.documentProcessorSettings = documentProcessorSettings;
        }

        protected ITemplatingEngine TemplatingEngine
        {
            get { return templatingEngine; }
        }

        protected ILogger Logger
        {
            get { return logger; }
        }

        protected DocumentProcessorSettings DocumentProcessorSettings
        {
            get { return documentProcessorSettings; }
        }

        private readonly ITemplatingEngine templatingEngine;
        private readonly ILogger logger;
        private readonly DocumentProcessorSettings documentProcessorSettings;
    }
}